package models

import (
	"course-srv/basic/config"
	"gorm.io/gorm"
)

// 课程
type Course struct {
	gorm.Model
	CourseTypeId int     `gorm:"type:int;comment:'课程分类id'"`
	MemberPrice  float64 `gorm:"type:decimal(10,2);comment:'会员价格'"`
	Price        float64 `gorm:"type:decimal(10,2);comment:'单价'"`
	Author       string  `gorm:"type:varchar(30);comment:'作者'"`
	Title        string  `gorm:"type:varchar(50);comment:'标题'"`
	Desc         string  `gorm:"type:varchar(300);comment:'描述'"`
	CoverUrl     string  `gorm:"type:varchar(300);comment:'视频封面'"`
	PayUrl       string  `gorm:"type:varchar(300);comment:'视频地址'"`
	Catalogue    string  `gorm:"type:varchar(50);comment:'课程目录'"`
}

func (c *Course) TableName() string {
	return "course"
}

// 添加课程
func (c *Course) AddCourse() error {
	return config.DB.Create(&c).Error
}

// 课程列表
func (c *Course) GetCourseList(page, pageSize int, courseTypeId int) (courseList []*Course, err error) {
	offest := (page - 1) * pageSize
	db := config.DB.Model(&Course{})

	if courseTypeId != 0 {
		db = db.Where("course_type_id = ?", courseTypeId)
	}

	if page > 0 || pageSize > 0 {
		db = db.Offset(offest).Limit(pageSize)
	}

	db.Find(&courseList)

	return courseList, err

}

// 课程详情
func (c *Course) GetCourseDetail(id int) error {
	return config.DB.Model(&Course{}).Where("id = ?", id).Limit(1).Find(&c).Error
}
